package com.lanou.moving.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lanou.moving.R;
import com.lanou.moving.entity.SongList;
import com.lanou.moving.utils.Exchange;
import java.util.List;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 刘琪 on 15/10/27.
 */
public class SongListGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<SongList.DataEntity> datas;

    public SongListGridViewAdapter(Context context, List<SongList.DataEntity> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void addDatas(List<SongList.DataEntity> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void refreshDatas(List<SongList.DataEntity> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public SongList.DataEntity getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.song_list_item, parent, false);
            holder = new ViewHolder();
            holder.song_list_item_img = (ImageView) convertView.findViewById(R.id.song_list_item_img);
            holder.song_list_item_title = (TextView) convertView.findViewById(R.id.song_list_item_title);
            holder.song_list_item_listen_count = (TextView) convertView.findViewById(R.id.song_list_item_listen_count);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SongList.DataEntity data = getItem(position);
        if (data.getListen_count() == 0) {
            holder.song_list_item_listen_count.setVisibility(View.GONE);
        } else {
            holder.song_list_item_listen_count.setVisibility(View.VISIBLE);
            String num = Exchange.getCount(data.getListen_count());
            holder.song_list_item_listen_count.setText(num);
        }
        holder.song_list_item_title.setText(data.getTitle());
        Picasso.with(context).load(data.getPic_url()).placeholder(R.mipmap.song_list_before)
                .error(R.mipmap.song_list_before).into(holder.song_list_item_img);
        return convertView;
    }

    private class ViewHolder {
        private ImageView song_list_item_img;
        private TextView song_list_item_title, song_list_item_listen_count;
    }

}
